#include "EmitterManager.h"
#include "../TinyXML/tinyxml.h"
#include <fstream>
#include <ShlObj.h>
#include "../SGD Wrappers/SGD_String.h"


/*static*/ EmitterManager* EmitterManager::GetInstance()
{
	static EmitterManager EM;
	return &EM;
}

/*static*/ void EmitterManager::DeleteInstance()
{
}

EmitterManager::EmitterManager(void)
{
}


EmitterManager::~EmitterManager(void)
{
	for (unsigned int i = 0; i < EmitterArray.size(); i++)
	{
		delete EmitterArray[i];
	}
}

void EmitterManager::Load( string emittername, string filename, bool isActive, float posX, float posY )
{
	Emitter* emitter;
	TiXmlDocument doc;

	/*wchar_t* localappdata = nullptr;

	SHGetKnownFolderPath( FOLDERID_LocalAppData, 0, NULL, &localappdata );

	wstringstream length;

	length << localappdata;

	int bytes = length.str().size();
	int strlength = length.str().length();
	int filenamelen = filename.length();

	char* notsowidenow = new char[strlength+1];

	WSTR_TO_CSTR(notsowidenow, bytes+1, localappdata);

	string loadme = notsowidenow;
	loadme += "\\Emitters\\";
	loadme += filename;*/

	string loadme = "XML\\Emitters\\";
	loadme += filename;

	doc.LoadFile(loadme.c_str());

	//CoTaskMemFree((void*)localappdata);

	TiXmlElement* root = doc.RootElement();

	TiXmlElement* image = root->FirstChildElement( "image" );
	string imagefilenameend = image->GetText();

	string imagefilename = "Textures\\Particles\\";
	imagefilename += imagefilenameend;

	/*string imagefilename = notsowidenow;
	imagefilename += "\\Particles\\";
	imagefilename += imagefilenameend;

	delete [] notsowidenow;*/

	//TiXmlElement* properties = root->FirstChildElement( "properties" );
	TiXmlElement* maxpar = root->FirstChildElement( "maxparticles" );

	double maxParticles;
	maxpar->Attribute( "particles", &maxParticles );

	emitter = new Emitter( imagefilename, (int)maxParticles );
	
	// These members are passed in from the Level Manager
	emitter->SetName( emittername );
	emitter->SetActive( isActive );
	emitter->SetPosX( posX );
	emitter->SetPosY( posX );
	//emitter->SetEmitterRotation( emitterRotation );

	TiXmlElement* shape = root->FirstChildElement( "shape" );
	string emitshape = shape->GetText();

	emitter->SetShape(emitshape);

	TiXmlElement* speed = root->FirstChildElement( "speed" );

	double maxspeed;
	speed->Attribute( "max", &maxspeed );
	double minspeed;
	speed->Attribute( "min", &minspeed );

	emitter->SetSpeedMin((float)minspeed);
	emitter->SetSpeedMax((float)maxspeed);

	TiXmlElement* lifetime = root->FirstChildElement( "lifetime" );

	double maxlifetime;
	lifetime->Attribute( "max", &maxlifetime );
	double minlifetime;
	lifetime->Attribute( "min", &minlifetime );

	emitter->SetLifetimeMin( (float)minlifetime );
	emitter->SetLifetimeMax( (float)maxlifetime );

	TiXmlElement* offset = root->FirstChildElement( "offset" );

	double offsetX;
	offset->Attribute( "x", &offsetX );
	double offsetY;
	offset->Attribute( "y", &offsetY );

	emitter->SetOffsetX( (float)offsetX );
	emitter->SetOffsetY( (float)offsetY );

	TiXmlElement* startcolor = root->FirstChildElement( "startcolor" );

	int startcolora;
	int startcolorr;
	int startcolorg;
	int startcolorb;	
	startcolor->Attribute( "a", &startcolora );
	startcolor->Attribute( "r", &startcolorr );
	startcolor->Attribute( "g", &startcolorg );
	startcolor->Attribute( "b", &startcolorb );

	emitter->SetInitClrA( startcolora );
	emitter->SetInitClrR( startcolorr );
	emitter->SetInitClrG( startcolorg );
	emitter->SetInitClrB( startcolorb );

	TiXmlElement* endcolor = root->FirstChildElement( "endcolor" );

	int endcolora;
	int endcolorr;
	int endcolorg;
	int endcolorb;	
	endcolor->Attribute( "a", &endcolora );
	endcolor->Attribute( "r", &endcolorr );
	endcolor->Attribute( "g", &endcolorg );
	endcolor->Attribute( "b", &endcolorb );

	emitter->SetFinalClrA( endcolora );
	emitter->SetFinalClrR( endcolorr );
	emitter->SetFinalClrG( endcolorg );
	emitter->SetFinalClrB( endcolorb );

	TiXmlElement* scale = root->FirstChildElement( "scale" );

	double scaleinit;
	scale->Attribute( "initial", &scaleinit );
	double scalefinal;
	scale->Attribute( "final", &scalefinal );

	emitter->SetInitialScale( (float)scaleinit );
	emitter->SetFinalScale( (float)scalefinal );

	TiXmlElement* rotation = root->FirstChildElement( "rotation" );

	double rotationinit;
	rotation->Attribute( "start", &rotationinit );
	double rotationfinal;
	rotation->Attribute( "end", &rotationfinal );

	emitter->SetInitialRotation( (float)rotationinit );
	emitter->SetFinalRotation( (float)rotationfinal );

	TiXmlElement* anglerange = root->FirstChildElement( "anglerange" );

	double angle;
	anglerange->Attribute( "range", &angle );

	emitter->SetAngleRange( (float)angle );

	TiXmlElement* emitterrotation = root->FirstChildElement( "emitterrotation" );

	double erot;
	emitterrotation->Attribute( "angle", &erot );

	emitter->SetEmitterRotation( (float)erot );

	TiXmlElement* gravity = root->FirstChildElement( "gravity" );
	string hasgravitystr = gravity->Attribute("hasgravity");

	if(hasgravitystr == "true")
		emitter->SetGravity( true );
	else
		emitter->SetGravity( false );

	TiXmlElement* looping = root->FirstChildElement( "looping" );
	string doesloopstr = looping->Attribute("doesloop");

	if(doesloopstr == "true")
		emitter->SetLooping( true );
	else
		emitter->SetLooping( false );

	TiXmlElement* spawnrate = root->FirstChildElement( "spawnrate" );

	double rate;
	spawnrate->Attribute( "timer", &rate );

	emitter->SetSpawnRate( (float)rate );

	emitter->ParticleInitialize();

	EmitterArray.push_back(emitter);
}

Emitter* EmitterManager::GetEmitter( string EmitterName )
{
	for(unsigned int i = 0; i < EmitterArray.size(); ++i )
	{
		if(EmitterArray[i]->GetName() == EmitterName)
			return EmitterArray[i];
	}
	return nullptr;
}

void EmitterManager::ClearEmitters()
{
	EmitterArray.clear();
}